home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / LSEQ101.ZIP / RCOPS.C < prev    next >
Text File  |  1990-06-20  |  10KB  |  427 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)rcops.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <stdlib.h>*/
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "lseq_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      ls_rcalloc - allocate memory for lseq record
  20.  
  21. SYNOPSIS
  22.      #include "lseq_.h"
  23.  
  24.      lsrec_t *ls_rcalloc(lsp)
  25.      lseq_t *lsp;
  26.  
  27. DESCRIPTION
  28.      The ls_rcalloc function creates a record of the appropriate
  29.      configuration for lseq lsp and initializes it.  The address of
  30.      the record created is returned.
  31.  
  32.      ls_rcalloc will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       lsp is not a valid lseq pointer.
  35.      [ENOMEM]       Not enough memory is available for
  36.                     allocation by the calling process.
  37.      [LSENOPEN]     lsp is not open.
  38.  
  39. SEE ALSO
  40.      ls_rcfree, ls_rcinit.
  41.  
  42. DIAGNOSTICS
  43.      On failure, a value of NULL is returned, and errno set to
  44.      indicate the error.
  45.  
  46. ------------------------------------------------------------------------------*/
  47. lsrec_t *ls_rcalloc(lsp)
  48. lseq_t *lsp;
  49. {
  50.     lsrec_t *lsrp = NULL;
  51. #ifdef DEBUG
  52.     /* validate arguments */
  53.     if (!ls_valid(lsp)) {
  54.         LSEPRINT;
  55.         errno = EINVAL;
  56.         return NULL;
  57.     }
  58.  
  59.     /* check if not open */
  60.     if (!(lsp->flags & LSOPEN)) {
  61.         LSEPRINT;
  62.         errno = LSENOPEN;
  63.         return NULL;
  64.     }
  65. #endif
  66.     /* allocate storage for main record structure */
  67.     /* (calloc is used throughout to automatically set all bits 0) */
  68.     lsrp = (lsrec_t *)calloc((size_t)1, sizeof(lsrec_t));
  69.     if (lsrp == NULL) {
  70.         LSEPRINT;
  71.         errno = ENOMEM;
  72.         return NULL;
  73.     }
  74.     lsrp->next = NIL;
  75.     lsrp->prev = NIL;
  76.     lsrp->recbuf = calloc((size_t)1, lsp->lshdr.recsize);
  77.     if (lsrp->recbuf == NULL) {
  78.         LSEPRINT;
  79.         free(lsrp);
  80.         errno = ENOMEM;
  81.         return NULL;
  82.     }
  83.  
  84.     errno = 0;
  85.     return lsrp;
  86. }
  87.  
  88. /*man---------------------------------------------------------------------------
  89. NAME
  90.      ls_rccopy - copy lseq record
  91.  
  92. SYNOPSIS
  93.      #include "lseq_.h"
  94.  
  95.      int ls_rccopy(lsp, tlsrp, slsrp)
  96.      lseq_t *lsp;
  97.      lsrec_t *tlsrp;
  98.      const lsrec_t *slsrp;
  99.  
  100. DESCRIPTION
  101.      The ls_rccopy function makes an exact copy of source record slsrp
  102.      in target record tlsrp.
  103.  
  104.      ls_rccopy will fail if one or more of the following is true:
  105.  
  106.      [EINVAL]       lsp is not a valid lseq pointer.
  107.      [EINVAL]       tlsrp or slsrp is the NULL pointer.
  108.  
  109. DIAGNOSTICS
  110.      Upon successful completion, a value of 0 is returned.  Otherwise,
  111.      a value of -1 is returned, and errno set to indicate the error.
  112.  
  113. ------------------------------------------------------------------------------*/
  114. int ls_rccopy(lsp, tlsrp, slsrp)
  115. lseq_t *lsp;
  116. lsrec_t *tlsrp;
  117. const lsrec_t *slsrp;
  118. {
  119. #ifdef DEBUG
  120.     /* validate arguments */
  121.     if (!ls_valid(lsp) || tlsrp == NULL || slsrp == NULL) {
  122.         LSEPRINT;
  123.         errno = EINVAL;
  124.         return -1;
  125.     }
  126. #endif
  127.     /* copy record slsrp into tlsrp */
  128.     tlsrp->next = slsrp->next;
  129.     tlsrp->prev = slsrp->prev;
  130.     memcpy(tlsrp->recbuf, slsrp->recbuf, lsp->lshdr.recsize);
  131.  
  132.     errno = 0;
  133.     return 0;
  134. }
  135.  
  136. /*man---------------------------------------------------------------------------
  137. NAME
  138.      ls_rcfree - free memory allocated for lseq record
  139.  
  140. SYNOPSIS
  141.      #include "lseq_.h"
  142.  
  143.      void ls_rcfree(lsrp)
  144.      lsrec_t *lsrp;
  145.  
  146. DESCRIPTION
  147.      The ls_rcfree function frees all memory allocated for lseq record
  148.      lsrp.
  149.  
  150. SEE ALSO
  151.      ls_rcalloc.
  152.  
  153. ------------------------------------------------------------------------------*/
  154. void ls_rcfree(lsrp)
  155. lsrec_t *lsrp;
  156. {
  157.     if (lsrp != NULL) {
  158.         if (lsrp->recbuf != NULL) {
  159.             free(lsrp->recbuf);
  160.             lsrp->recbuf = NULL;
  161.         }
  162.         free(lsrp);
  163.     }
  164.  
  165.     return;
  166. }
  167.  
  168. /*man---------------------------------------------------------------------------
  169. NAME
  170.      ls_rcget - lseq record get
  171.  
  172. SYNOPSIS
  173.      #include "lseq_.h"
  174.  
  175.      int ls_rcget(lsp, lspos, lsrp)
  176.      lseq_t *lsp;
  177.      lspos_t lspos;
  178.      lsrec_t *lsrp;
  179.  
  180. DESCRIPTION
  181.      The ls_rcget function reads the record at position lspos into the
  182.      record pointed to be lsrp.  The entire record is read, including
  183.      the links.
  184.  
  185. SEE ALSO
  186.      ls_rcput.
  187.  
  188. DIAGNOSTICS
  189.      Upon successful completion, a value of 0 is returned.  Otherwise,
  190.      a value of -1 is returned, and errno set to indicate the error.
  191.  
  192. ------------------------------------------------------------------------------*/
  193. int ls_rcget(lsp, lspos, lsrp)
  194. lseq_t *lsp;
  195. lspos_t lspos;
  196. lsrec_t *lsrp;
  197. {
  198.     void *buf = NULL;
  199. #ifdef DEBUG
  200.     /* validate arguments */
  201.     if (!ls_valid(lsp) || lsrp == NULL || lspos == NIL) {
  202.         LSEPRINT;
  203.         errno = EINVAL;
  204.         return -1;
  205.     }
  206.  
  207.     /* check if not open */
  208.     if (!(lsp->flags & LSOPEN)) {
  209.         LSEPRINT;
  210.         errno = LSENOPEN;
  211.         return -1;
  212.     }
  213. #endif
  214.     /* read record from file */
  215.     buf = calloc((size_t)1, ls_blksize(lsp));
  216.     if (buf == NULL) {
  217.         LSEPRINT;
  218.         errno = ENOMEM;
  219.         return -1;
  220.     }
  221.     if (bgetb(lsp->bp, (bpos_t)lspos, buf) == -1) {
  222.         LSEPRINT;
  223.         free(buf);
  224.         return -1;
  225.     }
  226.  
  227.     /* convert record from file format */
  228.     memcpy(lsrp, buf, offsetof(lsrec_t, recbuf));
  229.     memcpy(lsrp->recbuf, ((char *)buf + offsetof(lsrec_t, recbuf)), lsp->lshdr.recsize);
  230.  
  231.     /* free buffer */
  232.     free(buf);
  233.     buf = NULL;
  234.  
  235.     errno = 0;
  236.     return 0;
  237. }
  238.  
  239. /*man---------------------------------------------------------------------------
  240. NAME
  241.      ls_rcinit - lseq record initialize
  242.  
  243. SYNOPSIS
  244.      #include "lseq_.h"
  245.  
  246.      void ls_rcinit(lsp, lsrp)
  247.      lseq_t *lsp;
  248.      lsrec_t *lsrp;
  249.  
  250. DESCRIPTION
  251.      The ls_rcinit function initializes record lsrp.
  252.  
  253. ------------------------------------------------------------------------------*/
  254. void ls_rcinit(lsp, lsrp)
  255. lseq_t *lsp;
  256. lsrec_t *lsrp;
  257. {
  258. #ifdef DEBUG
  259.     /* validate arguments */
  260.     if (!ls_valid(lsp) || lsrp == NULL) {
  261.         LSEPRINT;
  262.         return;
  263.     }
  264. #endif
  265.     /* initialize lsrp */
  266.     lsrp->next = NIL;
  267.     lsrp->prev = NIL;
  268.     if (lsrp->recbuf == NULL) {
  269.         LSEPRINT;
  270.         return;
  271.     }
  272.  
  273.     memset(lsrp->recbuf, 0, lsp->lshdr.recsize);
  274.  
  275.     return;
  276. }
  277.  
  278. /*man---------------------------------------------------------------------------
  279. NAME
  280.      ls_rcput - lseq record put
  281.  
  282. SYNOPSIS
  283.      #include "lseq_.h"
  284.  
  285.      int ls_rcput(lsp, lspos, lsrp)
  286.      lseq_t *lsp;
  287.      lspos_t lspos;
  288.      const lsrec_t *lsrp;
  289.  
  290. DESCRIPTION
  291.      The ls_rcput function writes the record pointed to by lsrp into
  292.      record position lspos.  The entire record is written, including
  293.      the links.
  294.  
  295.      ls_rcput will fail if one or more of the following is true:
  296.  
  297.      [EINVAL]       lsp is not a valid lseq pointer.
  298.      [EINVAL]       lspos is NIL.
  299.      [LSENOPEN]     lsp is not open.
  300.  
  301. SEE ALSO
  302.      ls_rcget, ls_rcputf.
  303.  
  304. DIAGNOSTICS
  305.      Upon successful completion, a value of 0 is returned.  Otherwise,
  306.      a value of -1 is returned, and errno set to indicate the error.
  307.  
  308. ------------------------------------------------------------------------------*/
  309. int ls_rcput(lsp, lspos, lsrp)
  310. lseq_t *lsp;
  311. lspos_t lspos;
  312. const lsrec_t *lsrp;
  313. {
  314.     void *buf = NULL;
  315. #ifdef DEBUG
  316.     /* validate arguments */
  317.     if (!ls_valid(lsp) || lspos == NIL || lsrp == NULL) {
  318.         LSEPRINT;
  319.         errno = EINVAL;
  320.         return -1;
  321.     }
  322.  
  323.     /* check if not open */
  324.     if (!(lsp->flags & LSOPEN)) {
  325.         LSEPRINT;
  326.         errno = LSENOPEN;
  327.         return -1;
  328.     }
  329. #endif
  330.     /* convert record to file format */
  331.     buf = calloc((size_t)1, ls_blksize(lsp));
  332.     if (buf == NULL) {
  333.         LSEPRINT;
  334.         errno = ENOMEM;
  335.         return -1;
  336.     }
  337.     memcpy(buf, lsrp, offsetof(lsrec_t, recbuf));
  338.     memcpy(((char *)buf + offsetof(lsrec_t, recbuf)), lsrp->recbuf, lsp->lshdr.recsize);
  339.  
  340.     /* write record to file */
  341.     if (bputb(lsp->bp, (bpos_t)lspos, buf) == -1) {
  342.         LSEPRINT;
  343.         free(buf);
  344.         return -1;
  345.     }
  346.  
  347.     /* free buffer */
  348.     free(buf);
  349.     buf = NULL;
  350.  
  351.     errno = 0;
  352.     return 0;
  353. }
  354.  
  355. /*man---------------------------------------------------------------------------
  356. NAME
  357.      ls_rcputf - lseq record field put
  358.  
  359. SYNOPSIS
  360.      #include "lseq_.h"
  361.  
  362.      int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  363.      lseq_t *lsp;
  364.      lspos_t lspos;
  365.      size_t offset;
  366.      const void *buf;
  367.      size_t bufsize;
  368.  
  369. DESCRIPTION
  370.      The ls_rcputf function writes the field pointed to by buf into
  371.      record position lspos.  Only the field is written.
  372.  
  373.      ls_rcputf will fail if one or more of the following is true:
  374.  
  375.      [EINVAL]       lsp is not a valid lseq pointer.
  376.      [EINVAL]       lspos is NIL.
  377.      [LSEBOUND]
  378.      [LSENOPEN]
  379.  
  380. SEE ALSO
  381.      ls_rcget, ls_rcput.
  382.  
  383. DIAGNOSTICS
  384.      Upon successful completion, a value of 0 is returned.  Otherwise,
  385.      a value of -1 is returned, and errno set to indicate the error.
  386.  
  387. ------------------------------------------------------------------------------*/
  388. int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  389. lseq_t *lsp;
  390. lspos_t lspos;
  391. size_t offset;
  392. const void *buf;
  393. size_t bufsize;
  394. {
  395. #ifdef DEBUG
  396.     /* validate arguments */
  397.     if (!ls_valid(lsp) || lspos == NIL || buf == NULL || bufsize < 1) {
  398.         LSEPRINT;
  399.         errno = EINVAL;
  400.         return -1;
  401.     }
  402.  
  403.     /* check if not open */
  404.     if (!(lsp->flags & LSOPEN)) {
  405.         LSEPRINT;
  406.         errno = LSENOPEN;
  407.         return -1;
  408.     }
  409.  
  410.     /* check if record boundary crossed */
  411.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  412.         LSEPRINT;
  413.         errno = LSEBOUND;
  414.         return -1;
  415.     }
  416. #endif
  417.     /* write record to file */
  418.     if (bputbf(lsp->bp, (bpos_t)lspos, offsetof(lsrec_t, recbuf) + offset, buf, bufsize) == -1) {
  419.         LSEPRINT;
  420.         free(buf);
  421.         return -1;
  422.     }
  423.  
  424.     errno = 0;
  425.     return 0;
  426. }
  427.